const fs = require('fs');
const path = require('path');
const axios = require('axios');
const md5 = require('./md5.js');

//定义监听接口
// const readline = require('readline');
// 创建一个接口，监听用户输入
// const rl = readline.createInterface({
//     input: process.stdin,
//     output: process.stdout
// });

// // 提示用户输入
// rl.question('请按下按钮：', (answer) => {
//     rl.close();
// });

// E:/最新项目/le-qi/i18n/cn2.js


let objData = [];
let newObj = {};
//循环计数
let count = 0;
//当前要翻译的文件地址
let fileRoute = 'E:/全部项目管理/my原生项目/nodejsI18n百度翻译/i18n/'
let fileRoName = 'cn.js'
//当前要翻译成的几种,en英语，fra法语，de德语，it意大利，spa西班牙，cht繁体，pl波兰
// let langArray = [
// 	'en',
// 	'spa'
// ]
let langArray = [
	'en',
	'cht',
	'fra',
	'pl',
]
//当前执行到第几个语言
let langArrayNum = 0

//获取文件数据
function getReadFile() {
	return new Promise((resolve, reject) => {
		fs.readFile(fileRoute+fileRoName, 'utf-8', (err, data) => {
			if (err) throw err;
			let myObjaa = String(data)
			myObjaa = myObjaa.replace('export default', '')
			myObjaa = myObjaa.trim()

			let func = new Function(`return (${myObjaa})`);
			let obj = func();

			for (item in obj) {
				newObj[item] = {}
				for (item2 in obj[item]) {
					newObj[item][item2] = ''
					let aaaa = obj[item][item2];
					objData.push({
						key1: item,
						value: obj[item][item2],
						key2: item2,
					})
				}
			}

			resolve()

		});
	})
}


intervalIdS2()
async function intervalIdS2() {
	await getReadFile()
	intervalIdS()
}

//定时执行
async function intervalIdS() {
	return new Promise((resolve, reject) => {
		// let intervalId = setInterval(() => {
	
		// }, 1000);
		i18nFy(objData[count].value).then(e => {
			newObj[objData[count].key1][objData[count].key2] = e;
			count++;
			// console.log(count)
			if (count == objData.length) {
				//重置循环计数
				count = 0
				
				//保存的内容
				let fileContent = JSON.stringify(newObj)
				fileContent = `export default ${fileContent}`

				//判断要翻译的语言
				if(langArrayNum < langArray.length){
					fileSave(fileContent);
					intervalIdS();
				}
				
			}else{
				intervalIdS();
			}
		
		});
		
		
	})
}


//当前要翻译成的几种,en英语，fra法语，de德语，it意大利，spa西班牙
//要翻译的内容
function fileSave(fileContent){
	
	if(langArray[langArrayNum] == 'cht'){
		fs.writeFile(fileRoute+'cht.js', fileContent, err => {
			if (err) throw err;
			console.log('写入成功'+langArray[langArrayNum]);
			langArrayNum++
		});
	}
	
	if(langArray[langArrayNum] == 'en'){
		fs.writeFile(fileRoute+'en.js', fileContent, err => {
			if (err) throw err;
			console.log('写入成功'+langArray[langArrayNum]);
			langArrayNum++
		});
	}
	
	if(langArray[langArrayNum] == 'fra'){
		fs.writeFile(fileRoute+'french.js', fileContent, err => {
			if (err) throw err;
			console.log('写入成功'+langArray[langArrayNum]);
			langArrayNum++
		});
	}
	
	if(langArray[langArrayNum] == 'de'){
		fs.writeFile(fileRoute+'german.js', fileContent, err => {
			if (err) throw err;
			console.log('写入成功'+langArray[langArrayNum]);
			langArrayNum++
		});
	}
	
	if(langArray[langArrayNum] == 'it'){
		fs.writeFile(fileRoute+'ltalian.js', fileContent, err => {
			if (err) throw err;
			console.log('写入成功'+langArray[langArrayNum]);
			langArrayNum++
		});
	}
	
	if(langArray[langArrayNum] == 'spa'){
		fs.writeFile(fileRoute+'spanish.js', fileContent, err => {
			if (err) throw err;
			console.log('写入成功'+langArray[langArrayNum]);
			langArrayNum++
		});
	}
	
	if(langArray[langArrayNum] == 'pl'){
		fs.writeFile(fileRoute+'polish.js', fileContent, err => {
			if (err) throw err;
			console.log('写入成功'+langArray[langArrayNum]);
			langArrayNum++
		});
	}

}






//接口获取翻译数据
function i18nFy(Str) {
	return new Promise((resolve, reject) => {
		var appid = '20211108000993824';
		var key = 'tsqDuzlC2atualgOdZgX';
		var salt = (new Date).getTime();
		var query = String(Str);
		// 多个query可以用\n连接  如 query='apple\norange\nbanana\npear'
		var from = 'zh';
		var to = langArray[langArrayNum];
		var str1 = appid + query + salt + key;
		var sign = md5(str1);
		axios.get('http://api.fanyi.baidu.com/api/trans/vip/translate', {
			params: {
				q: query,
				appid: appid,
				salt: salt,
				from: from,
				to: to,
				sign: sign
			}
		}).then(function(res) {

			console.log(res.data.trans_result[0].dst);
			resolve(res.data.trans_result[0].dst)
			// console.log(JSON.parse(res.data.trans_result[0].dst));
			// resolve(data.trans_result[0].dst)

		}).catch(function(error) {
			console.log(error);
		});

	})
}